package com.aizuda.boot.modules.business.contract.controller;

import com.aizuda.boot.modules.business.contract.dto.SingleResponse;
import com.aizuda.boot.modules.business.contract.entity.dto.*;
import com.aizuda.boot.modules.business.contract.entity.vo.ContractWorkflowListPageDTO;
import com.aizuda.boot.modules.business.contract.entity.vo.FieldTreeDTO;
import com.aizuda.boot.modules.business.contract.entity.vo.WorkflowShowDTO;
import com.aizuda.boot.modules.business.contract.service.ContractWorkflowFormFieldService;
import com.aizuda.boot.modules.business.contract.service.impl.ContractExe;
import com.baomidou.mybatisplus.core.metadata.IPage;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author sangyirong
 * @ClassName ContractWorkflowController
 * @Version 1.0
 * @Date 2025/5/29 18:14
 */
@RestController
@RequestMapping("/contract/workflows")
@Tag(name= "合同流程管理接口")
public class ContractWorkflowController {

    @Autowired
    private ContractExe contractExe;

    @Resource
    private ContractWorkflowFormFieldService contractWorkflowFormFieldService;


    @Operation(summary = "获取合同流程列表")
    @PostMapping("/{pageNo}/{PageSize}")
    public SingleResponse<IPage<ContractWorkflowListPageDTO>> listContractWorkflows(@PathVariable Integer pageNo,
                                                                                    @PathVariable Integer PageSize,
                                                                                    @RequestBody ContractWorkflowListQueryDTO contractWorkflowListQueryDTO) {
        return contractExe.listContractWorkflows(pageNo, PageSize, contractWorkflowListQueryDTO);
    }

    @Operation(summary = "新建合同流程")
    @PostMapping("")
    public SingleResponse createContractWorkflow(@RequestBody ContractWorkflowAddDTO workflowAddDTO) {
        return contractExe.createContractWorkflow(workflowAddDTO);
    }

    @Operation(summary = "删除合同流程")
    @DeleteMapping("/{id}")
    public SingleResponse deleteContractWorkflow(@PathVariable Integer id) {
        return contractExe.deleteContractWorkflow(id);
    }

    @Operation(summary = "复制合同流程")
    @Deprecated
    @PostMapping("/{id}/copy")
    public SingleResponse<WorkflowShowDTO> copyContractWorkflow(@PathVariable Integer id, @RequestBody CopyWorkflowDTO copyWorkflowDTO) {
        return contractExe.copyContractWorkflow(id,copyWorkflowDTO);
    }

    @Operation(summary = "获取合同流程详情")
    @GetMapping("/{id}/detail")
    public SingleResponse<WorkflowShowDTO> getDetail(@PathVariable Integer id) {
        return contractExe.getDetail(id);
    }



    @Operation(summary = "保存合同流程_新")
    @PostMapping("/save/{workflowId}")
    public SingleResponse saveWorkflow(@PathVariable Long workflowId, @RequestBody WorkflowSaveDTO workflowSaveDTO) {
        return contractExe.saveWorkflow(workflowId, workflowSaveDTO);
    }

    @Operation(summary = "更新合同流程")
    @PutMapping("/update")
    public SingleResponse updateWorkflow(@RequestBody @Validated WorkflowUpdateDTO workflowUpdateDTO){
        return contractExe.updateWorkflow(workflowUpdateDTO);
    }

    // ==================== 审批节点管理 ====================

    @Operation(summary = "发布合同流程")
    @PutMapping("/{workflowId}/publish")
    public SingleResponse publishContractWorkflow(@PathVariable Long workflowId,  @RequestBody WorkflowSaveDTO workflowSaveDTO) {
        return contractExe.publishContractWorkflow(workflowId, workflowSaveDTO);
    }

    // ==================== 审批相关选项管理 ====================

    @Operation(summary = "获取空审批人策略")
     @GetMapping("/emptyAssignee")
     public SingleResponse getEmptyAssignee() {
        return contractExe.getEmptyAssignee();
    }

    @Operation(summary = "获取自我审批策略")
    @GetMapping("/selfApproval")
    public SingleResponse getSelfApproval() {
        return contractExe.getSelfApproval();
    }

    @Operation(summary = "添加流程模块关联")
    @PostMapping("/{workflowId}/modules")
    public SingleResponse addWorkflowModules(@PathVariable Long workflowId, @RequestBody WorkflowModuleDTO workflowModuleDTO) {
        return contractExe.addWorkflowModules(workflowId, workflowModuleDTO);
    }

    @Operation(summary = "删除流程模块关联")
    @DeleteMapping("/{workflowId}/modules")
    public SingleResponse deleteWorkflowModules(@PathVariable Long workflowId, @RequestBody WorkflowModuleDTO workflowModuleDTO) {
        return contractExe.deleteWorkflowModules(workflowId, workflowModuleDTO);
    }

    @Operation(summary = "获取所有启用模块的字段树")
    @GetMapping("/fieldTree/{workflowId}")
    public SingleResponse<List<FieldTreeDTO>> getFieldTree(@PathVariable Long workflowId){
        return contractWorkflowFormFieldService.getFieldTree(workflowId);
    }


    @Operation(summary = "更新流程配置状态")
    @PutMapping("/status/{workflowId}")
    public SingleResponse<Boolean> updateStatus(@PathVariable Long workflowId){
        return contractExe.updateStatus(workflowId);
    }
}
